using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class ShakeAnimation : MonoBehaviour
{
	public List<ShakeStep> animationSteps = new List<ShakeStep>();

	private int step;

	private float stepTime;

	private float initialAngle;

	private ShakeStep currentStep;

	private bool playing;

	public UnityEvent onAnimationEnd = new UnityEvent();

	public void Play()
	{
		step = -1;
		StartCoroutine(DelayShakeAnimation());
		SetNextStep();
	}

	private IEnumerator DelayShakeAnimation()
	{
		yield return new WaitForSeconds(UnityEngine.Random.Range(0f, 0.2f));
		playing = true;
	}

	private void Update()
	{
		if (playing)
		{
			stepTime += Time.deltaTime;
			float num = Mathf.Lerp(initialAngle, currentStep.endAngle, stepTime / currentStep.stepTime);
			base.transform.localEulerAngles = Vector3.forward * num;
			if (num == currentStep.endAngle)
			{
				SetNextStep();
			}
		}
	}

	private void SetNextStep()
	{
		step++;
		if (step >= animationSteps.Count)
		{
			onAnimationEnd.Invoke();
			playing = false;
			return;
		}
		stepTime = 0f;
		Vector3 localEulerAngles = base.transform.localEulerAngles;
		initialAngle = localEulerAngles.z;
		if (initialAngle > 180f)
		{
			initialAngle -= 360f;
		}
		currentStep = animationSteps[step];
	}
}
